package com.ns002.yigou.quote.common.scheduled;

import com.ns002.yigou.quote.domain.Product;
import com.ns002.yigou.quote.entity.KLine;
import com.ns002.yigou.quote.service.KLineService;
import com.ns002.yigou.quote.service.ProductService;
import com.opslab.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author xu
 * @Date 2017/7/28 0028 上午 11:29
 * K线生成
 */
@Component
public class KLineScheduled {

    @Autowired
    KLineService kLineService;
    @Autowired
    ProductService productService;
    private static final Logger log = LoggerFactory.getLogger("kline");

    @Value("${quote.save}")
    private boolean quoteSvae;

    /**
     * 一分钟K线
     */
    @Scheduled(cron="1 0/1 * ? * *")
    public void m1(){

        if(!quoteSvae){
            return;
        }

        List<Product> productList = productService.findProductList();
        Date current = new Date();
        long space = 60000 ;
        Date quoteDate = new Date(current.getTime() - (current.getTime() % space + space));  //获取上一分钟的行情

        for (Product product: productList ) {
            try{
                KLine kLine = kLineService.findKLineNode(product.getContract(), quoteDate, "M1");
                if(kLine == null){
                    log.error("当前时间无点位更新-" + DateUtil.dateTime(quoteDate) + "-" + product.getContract()  + "-M1");
                    return;
                }
                kLineService.save(kLine);
            } catch (Exception e){
                log.error("K线查询失败" + product.getContract(), e);
            }
        }

        //更新分时图缓存
        kLineService.updateM1Cache();
    }


    /**
     * 5分钟K线
     */
    @Scheduled(cron="1 0/5 * * * ?")
    public void m5(){

        if(!quoteSvae){
            return;
        }

        List<Product> productList = productService.findProductList();
        Date current = new Date();
        long space = 60000 * 5;
        Date quoteDate = new Date(current.getTime() - (current.getTime() % space + space));  //获取上一分钟的行情

        for (Product product: productList ) {
            try{
                KLine kLine = kLineService.findKLineNode(product.getContract(), quoteDate, "M5");
                if(kLine == null){
                    log.error("当前时间无点位-" + DateUtil.dateTime(quoteDate) + "-" + product.getContract()  + "-M5");
                    return;
                }
                kLineService.save(kLine);
            } catch (Exception e){
                log.error("K线查询失败" + product.getContract(), e);
            }
        }

        kLineService.updateKLineCache("M5");
    }

    /**
     * 15分钟K线
     */
    @Scheduled(cron="1 0/15 * * * ?")
    public void m15() {

        if(!quoteSvae){
            return;
        }

        List<Product> productList = productService.findProductList();
        Date current = new Date();
        long space = 60000 * 15;
        Date quoteDate = new Date(current.getTime() - (current.getTime() % space + space));  //获取上一分钟的行情

        for (Product product: productList ) {
            try{
                KLine kLine = kLineService.findKLineNode(product.getContract(), quoteDate, "M15");
                if(kLine == null){
                    log.error("当前时间无点位-" + DateUtil.dateTime(quoteDate) + "-" + product.getContract()  + "-M15");
                    return;
                }
                kLineService.save(kLine);
            } catch (Exception e){
                log.error("K线查询失败" + product.getContract(), e);
            }

        }

        kLineService.updateKLineCache("M15");
    }

    /**
     * 30分钟K线
     */
    @Scheduled(cron="1 0/30 * * * ?")
    public void m30(){

        if(!quoteSvae){
            return;
        }

        List<Product> productList = productService.findProductList();
        Date current = new Date();
        long space = 60000 * 30;
        Date quoteDate = new Date(current.getTime() - (current.getTime() % space + space));  //获取上一分钟的行情

        for (Product product: productList ) {
            try {
                KLine kLine = kLineService.findKLineNode(product.getContract(), quoteDate, "M30");
                if(kLine == null){
                    log.error("当前时间无点位-" + DateUtil.dateTime(quoteDate) + "-" + product.getContract()  + "-M30");
                    return;
                }
                kLineService.save(kLine);
            } catch (Exception e) {
                log.error("K线查询失败" + product.getContract(), e);
            }

        }

        kLineService.updateKLineCache("M30");
    }


    /**
     * 1小时K线
     */
    @Scheduled(cron="1 0 0/1 * * ?")
    public void H1(){

        if(!quoteSvae){
            return;
        }

        List<Product> productList = productService.findProductList();
        Date current = new Date();
        long space = 60000 * 60;
        Date quoteDate = new Date(current.getTime() - (current.getTime() % space + space));  //获取上一分钟的行情

        for (Product product: productList ) {
            try{
                KLine kLine = kLineService.findKLineNode(product.getContract(), quoteDate, "H1");
                if(kLine == null){
                    log.error("当前时间无点位-" + DateUtil.dateTime(quoteDate) + "-" + product.getContract()  + "-H1");
                    return;
                }
                kLineService.save(kLine);
            } catch (Exception e){
                log.error("K线查询失败" + product.getContract(), e);
            }

        }

        kLineService.updateKLineCache("H1");
    }

    /**
     * 2小时K线
     */
    @Scheduled(cron="1 0 0/2 * * ?")
    public void H2(){

        if(!quoteSvae){
            return;
        }

        List<Product> productList = productService.findProductList();
        Date current = new Date();
        long space = 60000 * 60 * 2;
        Date quoteDate = new Date(current.getTime() - (current.getTime() % space + space));  //获取上一分钟的行情

        for (Product product: productList ) {
            try{
                KLine kLine = kLineService.findKLineNode(product.getContract(), quoteDate, "H2");
                if(kLine == null){
                    log.error("当前时间无点位-" + DateUtil.dateTime(quoteDate) + "-" + product.getContract()  + "-H2");
                    return;
                }
                kLineService.save(kLine);
            } catch (Exception e){
                log.error("K线查询失败" + product.getContract(), e);
            }

        }

        kLineService.updateKLineCache("H2");
    }

    /**
     * 4小时K线
     */
    @Scheduled(cron="1 0 0/4 * * ?")
    public void H4(){

        if(!quoteSvae){
            return;
        }

        List<Product> productList = productService.findProductList();
        Date current = new Date();
        long space = 60000 * 60 * 4;
        Date quoteDate = new Date(current.getTime() - (current.getTime() % space + space));  //获取上一分钟的行情

        for (Product product: productList ) {
            try{
                KLine kLine = kLineService.findKLineNode(product.getContract(), quoteDate, "H4");
                if(kLine == null){
                    log.error("当前时间无点位-" + DateUtil.dateTime(quoteDate) + "-" + product.getContract() + "-H4");
                    return;
                }
                kLineService.save(kLine);
            } catch (Exception e){
                log.error("K线查询失败" + product.getContract(), e);
            }

        }

        kLineService.updateKLineCache("H4");
    }


    /**
     * 一天K线
     */
    @Scheduled(cron="1 0 0 * * ?")
    public void D1(){

        if(!quoteSvae){
            return;
        }

        List<Product> productList = productService.findProductList();
        Date current = new Date();
        long space = 60000 * 60 * 24;
        Date quoteDate = new Date(current.getTime() - (current.getTime() % space + space));  //获取上一分钟的行情

        for (Product product: productList ) {
            try{
                KLine kLine = kLineService.findKLineNode(product.getContract(), quoteDate, "D1");
                if(kLine == null){
                    log.error("当前时间无点位-" + DateUtil.dateTime(quoteDate) + "-" + product.getContract() + "-D1");
                    return;
                }
                kLineService.save(kLine);
            } catch (Exception e){
                log.error("K线查询失败" + product.getContract(), e);
            }

        }

        kLineService.updateKLineCache("D1");
    }
}
